<?php

/**
 * Flickr Photosets - Index Page
 * Displays information from your friends.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets
*/
$fp = new FlickrPhotosetsCanvas(FlickrPhotosetsCanvas::uid_from_viewer, false, false, false);

if ($fp->uid) {
	
	// Get info and initialize
	$fp->get_info();
	
	if ($fp->is_flickr_authorized()) {
		$fp->init_flickr();
		$fp->flickr->setToken($fp->config['flickr_token']);

		// Prepare contents for your recent photos
		$own_recent_photos = $fp->flickr->photos_search(array('user_id' => $fp->nsid, 'per_page' => 6));

		// Prepare contents for recent photos from contacts
		$contacts_recent_photos = $fp->flickr->photos_getContactsPhotos(6, 1);
	}
		
}

// Load stylesheet
echo $fp->get_stylesheet('index');

?>

<fb:if-is-app-user>
	
	<?=$fp->get_header('home', $fp->application_name, true)?>
	
	<?php if (!$fp->is_flickr_authorized()): ?>
		<div class="boxes clearfix">
			<div class="left_column">

				<div class="box" id="account_box">
					<h3>Flickr Account</h3>
					<div class="inside">

						<p>Looks like you haven't set up your Flickr account yet. Click on the Authenticate button below to setup your Flickr account. When you click Authenticate, you will be redirected to Flickr to authorize access.</p>
						
						<form action="<?=$fp->callback_url?>/auth.php">
							<div class="buttons">
								<input type="hidden" name="write" />
								<input type="submit" class="button" value="Authenticate" />
							</div>
						</form>

					</div>
				</div>

			</div>
		</div>
	<?php else: ?>
		
		<?php if (isset($_GET['added'])) {
			echo '<fb:success><fb:message>Your Flickr account was added!</fb:message>You can go to the <a href="'.$fp->canvas_url.'/configure/">configuration page</a> to change your settings.</fb:success>';
		} ?>
		
		<?php if (isset($_GET['profileupdated'])): ?>
			<div style="overflow: hidden; margin-bottom: -21px;" id="profile_updated"><p class="message">Your profile was successfully updated.</p></div>
			<script type="text/javascript">
				Animation(document.getElementById('profile_updated')).duration(3000).checkpoint().to('height', '0px').to('opacity', 0).to('margin-bottom', 0).hide().ease(Animation.ease.end).go();
			</script>
		<?php endif; ?>
		
		<div class="boxes clearfix">
			<div class="left_column" style="margin-top: -16px;">
				
				<div id="recent_photos">
				<div class="grid_header"><h3>Your recent photos</h3></div>
					<div class="photo_grid">
						<table>
							<tr><? $i = 0; foreach ($own_recent_photos['photo'] as $photo): ?>
								<td><a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/<?=$photo['id']?>/">
									<fb:user-agent includes="ie 6">
										<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'thumbnail')?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
									</fb:user-agent>
									<fb:user-agent excludes="ie 6">
										<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'small')?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
									</fb:user-agent>
								</a></td>
							<? $i++;
							if ($i % 3 == 0) {
								echo '</tr><tr>';
							}
							endforeach; ?></tr>
						</table>
					</div>
				</div>

				<div id="contacts_recent" style="margin-top: 14px;">
					<div class="grid_header"><h3>Your contacts' recent photos</h3></div>
					<div class="photo_grid">
						<table>
							<tr><? $i = 0; foreach ($contacts_recent_photos as $photo): ?>
								<td>
									<? if ($uid = $fp->get_uid_from_nsid($photo['owner'])) {
										$url = $fp->canvas_url.'/photos/'.$uid.'/';
									} else {
										$url = 'http://www.flickr.com/photos/'.$photo['owner'].'/'.$photo['id'].'/';
									} ?>
									<a href="<?=$url?>">
									<fb:user-agent includes="ie 6">
										<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'thumbnail')?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?> by <?=$photo['username']?>" />
									</fb:user-agent>
									<fb:user-agent excludes="ie 6">
										<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'small')?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?> by <?=$photo['username']?>" />
									</fb:user-agent>
									</a>
								</td>
							<? $i++;
							if ($i % 3 == 0) {
								echo '</tr><tr>';
							}
							endforeach; ?></tr>
						</table>
					</div>
				</div>
				
			</div>
			<div class="right_column">
				
				<p class="message">What would you like to see on this page? <a href="http://flickrphotosets.tumblr.com/post/49967504/redesign-launch">Send us your feedback!</a></p>
				
				<?php if ($fp->beta): ?>
				<div class="box" id="profile_box">
					<h3>Profile Box</h3>
					<div class="inside">
						<p>Your profile box was last updated <?=date('F j, Y \a\\t g:i A (T)', $fp->get_last_profile_update())?></p>
						<p><a href="<?=$fp->canvas_url?>/update/?fromindex">Update Now</a></p>
					</div>
				</div>
				<?php endif; ?>
				
			</div>
		</div>
		
		<div class="clearfix" style="margin-top: 29px;"></div>
		
	<?php endif; ?>
	
<fb:else>
	
	<?=$fp->get_header('home', '<fb:application-name />', true)?>
	
	<div class="big_indent bottom_margin">
		<h1>Welcome to <fb:application-name />!</h1>
		
		<p>To get started, you'll need to <a href="<?=$fp->facebook->get_login_url('', true)?>">authorize your Flickr account</a>.</p>
		
		<p><fb:application-name /> allows you to display your <a href="http://www.flickr.com">Flickr</a> photos inside Facebook. You do not need to authorize the app in order to view photos from your friends with Flickr accounts.</p>
		
	</div>
	
</fb:else></fb:if-is-app-user>

<?=$fp->echo_footer(true)?>